import {Component, OnInit} from '@angular/core';
import {BasePage} from '../../common/basePage';
import {Events, NavController, ToastController} from '@ionic/angular';
import {HttpService} from '../../services/http.service';
import {UrlAndOPT} from '../../common/url-and-opt.enum';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {requiredZero} from '../../common/validators';
import {ActivatedRoute} from '@angular/router';
import {AppConfig} from '../../common/app-config.enum';

/**
 * 会员信息(个人)
 */
@Component({
  selector: 'app-membership-info',
  templateUrl: './membership-info.page.html',
  styleUrls: ['./membership-info.page.scss'],
})
export class MembershipInfoPage extends BasePage implements OnInit {

  optionMap: any = {}; // 问题待选项
  userInfo: any = {}; // 用户答案信息
  formModel: FormGroup;

  constructor(
    public navCtrl: NavController,
    public toastCtrl: ToastController,
    private fb: FormBuilder,
    private events: Events,
    private routeInfo: ActivatedRoute,
    private service$: HttpService
  ) {
    super(navCtrl, toastCtrl);
    this.formModel = fb.group({
      education: [0, [requiredZero]],
      marital: [0, [requiredZero]],
      workExperience: [0, [requiredZero]],
      annualIncome: [0, [requiredZero]],
      netAsset: [0, [requiredZero]],
      car: [0, [requiredZero]],
      house: [0, [requiredZero]],
      isEmployee: [0, [Validators.required]],
      emergencyContactType: [0],
      emergencyContactName: [0],
      emergencyContactMobile: [0]
    });
  }

  ngOnInit() {
    this.routeInfo.data
      .subscribe(resolve => {
        if (resolve['data']) {
          this.optionMap = resolve['data'];
          // 问题答案
          this.userInfo = resolve['data']['userInfo'];
          setTimeout(() => {
            this.formModel.patchValue({
              education: this.userInfo['education'],
              marital: this.userInfo['marital'],
              workExperience: this.userInfo['workExperience'],
              annualIncome: this.userInfo['annualIncome'],
              netAsset: this.userInfo['netAsset'],
              car: this.userInfo['car'],
              house: this.userInfo['house'],
              isEmployee: this.userInfo['isEmployee'],
              emergencyContactType: this.userInfo['emergencyContactType'],
              emergencyContactName: this.userInfo['emergencyContactName'] || '',
              emergencyContactMobile: this.userInfo['emergencyContactMobile']
            });
          }, 0);
        } else {
          this.goBack();
        }
      });
  }

  doSave() {
    if (this.formModel.valid) {
      const formValue = {...this.formModel.value, isEmployee: this.formModel.value['isEmployee'] ? 1 : 0};
      this.service$.doRequest(UrlAndOPT.auth_membership_submit, formValue)
        .subscribe(obj => {
          if (obj) {
            // 发出流程变化通知
            this.events.publish(AppConfig.TOPIC_UNDO_LIST);
            this.showToast('保存成功');
            this.goBack();
          }
        });
    } else {
      this.showToast('请完善信息后保存');
    }
  }

}
